package com.mineplatform.service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mineplatform.dao.M_UserDao;
import com.mineplatform.dao.M_UserReplyDao;
import com.mineplatform.domain.Consultation;
import com.mineplatform.domain.M_User;
import com.mineplatform.domain.M_UserReply;
import com.mineplatform.domain.UserReply;
import com.mineplatform.exception.FromUserNotExistException;
import com.mineplatform.exception.PriceNullOrEmptyException;
import com.mineplatform.exception.ReplyContentException;
import com.mineplatform.exception.ToUserNotExistException;
import com.mineplatform.exception.UserNotExistException;
import com.mineplatform.iservice.IM_UserReplyService;

@Service
public class M_UserReplyService implements IM_UserReplyService {

	private M_UserReplyDao _m_userreplydao;
	private M_UserDao _m_userdao;

	@Autowired
	public M_UserReplyService(M_UserReplyDao m_userreplydao, M_UserDao m_userdao) {
		this._m_userreplydao = m_userreplydao;
		this._m_userdao = m_userdao;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void reply(String from, String to, String replycontent)
			throws FromUserNotExistException, ToUserNotExistException, ReplyContentException {
		// TODO Auto-generated method stub

		M_User fromUser = this._m_userdao.findById(from);
		if (fromUser == null) {

			throw new FromUserNotExistException();
		}
		M_User toUser = this._m_userdao.findById(to);
		if (toUser == null) {

			throw new ToUserNotExistException();
		}
		if ((replycontent == null) || (replycontent.trim() == "")) {

			throw new ReplyContentException();
		}
		M_UserReply r = new M_UserReply();
		r.setId(UUID.randomUUID().toString());
		r.setFrom(from);
		r.setTo(to);
		r.setReplyTime(new Date());
		r.setReplyContent(replycontent);
		
		this._m_userreplydao.save(r);

	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public List<UserReply> getreplies(String userid) throws UserNotExistException {
		

		M_User user = this._m_userdao.findById(userid);
		if (user == null) {

			throw new UserNotExistException();
		}
		// TODO Auto-generated method stub
		return this._m_userreplydao.getreplies(userid);
	}
}
